package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.analyse.tool.SubstrLogic;
import com.kdtech.analyse.tool.SubstrType;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;

/**
 *  QingDaoNewsAnalyse  青岛新闻网解析类
 *  xiaonie 2012-11-22
 */
public class QingDaoNewsAnalyse implements AnalyseNews{
	private final String[] regexs={
			"http://[a-z]*[.]qingdaonews.com/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*[_0-9]*[.]htm",
			"http://[a-z]*[.]qingdaonews.com/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*[_0-9[all]]*[.]htm",
	};
	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {



		if (!isDetailPage(urlMeta.getUrl())) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";;
		String author="";
		String url=urlMeta.getUrl();
		Long date=null;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		Document doc=Jsoup.parse(html);
		doc.select("#displaypagenum").remove(); //翻页代码
		doc.select("p.cDGray.fc.f12").remove(); //翻页提示
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		ParseTool tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("div.padding h1"));
		tool.addParseLogic(new ParseLogic("td.title"));
		tool.addParseLogic(new ParseLogic("h1"));
		title=tool.parse();


		/*
		 * 解析时间
		 */
		tool.addParseLogic(new ParseLogic("div.sourcetime"));
		tool.addParseLogic(new ParseLogic("div.fr"));
		date=tool.parseDate();


		/*
		 * 解析内容
		 */
		tool.addParseLogic(new ParseLogic("div#endText"));
		tool.addParseLogic(new ParseLogic("div.text"));
		tool.addParseLogic(new ParseLogic("span.text1"));
		content=tool.parse();


		/*
		 * 解析作者
		 */
		tool.addParseLogic(new ParseLogic("div#container div.sourcetime strong",new SubstrLogic(SubstrType.after,"来源：")));
		author=tool.parse();
		if(StringUtils.isNotBlank(author)){
			author=author.trim();
		}
		if(StringUtils.isBlank(author)){
			author=JSoupUtils.matchAuthor(doc, "来源：");
		}

		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setAuthor(author);


		return meta;

	}

	public static void main(String[] args) {
		QingDaoNewsAnalyse a=new QingDaoNewsAnalyse();
		String url="http://news.qingdaonews.com/qingdao/2014-10/22/content_10730437.htm";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(a.isDetailPage(url));
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return true;
	}
}
